﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
namespace MonitorPlan
{
    public partial class Transaction : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        // send sms
        [System.Web.Services.WebMethod]
        public static string SendSms(int hid, string key)
        {
            string info="";
            Model.Tool t = new Model.Tool();
            string ToolId = new Model.Tool().GetToolIdByKey(hid, key);
            var notification = new Model.Notification();
            // get transaction key
            if (t.IsKey(hid, key) == "on")
            {
                
                if(notification.NotifySms(hid.ToString(),ToolId,"on")==true)
                {
                    Model.Tool tool = new Model.Tool().GetTool(ToolId.ToString(), hid.ToString());
                    info = tool.Id + "," + tool.Name + ",On";
                    string phone = new Model.Contact().GetPhone(hid.ToString());
                    if (phone != "")
                    {
                        info += ":" + phone;
                    }
                    else
                    {
                        info = "no phone";
                    }
                }
                else
                {
                    info = "no sms";
                }

            }
            else if (t.IsKey(hid, key) == "off")
            {
                if(notification.NotifySms(hid.ToString(),ToolId,"off")==true)
                {
                    Model.Tool tool = new Model.Tool().GetTool(ToolId.ToString(), hid.ToString());
                    info = tool.Id + "," + tool.Name + ",Off";
                    string phone = new Model.Contact().GetPhone(hid.ToString());
                    if (phone != "")
                    {
                        info += ":" + phone;
                    }
                    else
                    {
                        info = "no phone";
                    }
                }
                else
                {
                    info = "no sms";
                }
                
            }
            else
            {
                info = "no sms";
            }
           

            // return info: 2ab,Front Door, On:1323213,12312312
            return info;
        }
        // send email
        [System.Web.Services.WebMethod]
        public static string SendEmail(int hid, string key)
        {
            Model.Tool t = new Model.Tool();
            string ToolId = new Model.Tool().GetToolIdByKey(hid, key);
            // get transaction key
            if (t.IsKey(hid, key) == "on")
            {
                var notification = new Model.Notification();
                notification.Notify(hid.ToString(), ToolId, "on");

            }
            else if (t.IsKey(hid, key) == "off")
            {
                var notification = new Model.Notification();
                notification.Notify(hid.ToString(), ToolId, "off");
            }
            return "ok";
        }

        // alarm notification
        [System.Web.Services.WebMethod]
        public static string Alarm(int hid, string key)
        {
            string IsAlarm = "No";
            Model.Tool t = new Model.Tool();
            string ToolId = new Model.Tool().GetToolIdByKey(hid, key);
            // get transaction key
            if (t.IsKey(hid, key) == "on")
            {
                // alarm when the tool is on
                var notification = new Model.Notification();
                bool a = notification.NotifyAlarm(hid.ToString(), ToolId, "on");
                if (a == true)
                    IsAlarm = "OK";

            }
            else if (t.IsKey(hid, key) == "off")
            {
                // alarm when the tool is of
                var notification = new Model.Notification();
                bool a = notification.NotifyAlarm(hid.ToString(), ToolId, "off");
                if (a == true)
                    IsAlarm = "OK";
            }
            // return OK if the tool need to alarm
            return IsAlarm;
        }

        // the transaction method
        [System.Web.Services.WebMethod]
        public static String TranSact(int hid, string key)
        {
            
            string sms = "";
            string ToolId = new Model.Tool().GetToolIdByKey(hid, key);

            string keyon="", keyoff="", img="";
            Model.Tool t = new Model.Tool();

            // get transaction key
            if(t.IsKey(hid, key)=="on")
            {
                keyon = key;
                img = "lighton.png";
                
            }
            else if(t.IsKey(hid,key)=="off")
            {
                keyoff = key;
                img = "lightoff.png";
            }
            // transaction date
            string date = DateTime.Now.ToShortDateString();

            // am / pm time
            int h, m, s;
            string ampm = "am";
            h = DateTime.Now.Hour;
            m = DateTime.Now.Minute;
            s = DateTime.Now.Second;
            if (h >= 12)
                ampm = "pm";
            if (h > 12)
                h = h - 12;
            // transaction time
            string time = h+":" + m +":" + s;

            string sql = @"insert into [transaction](toolid,houseid,keyon,keyoff,date,time,ampm,light) ";
            sql += @"values('" + ToolId + "'," + hid +",'" + keyon +"','"+keyoff+"','" + date +"','"+time+"','"+ampm+"','" + img +"')";
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = sql;
          
            //cmd.Parameters.AddWithValue("@tid", ToolId);
            //cmd.Parameters.AddWithValue("@hid", hid);
            //cmd.Parameters.AddWithValue("@onkey", keyon);
            //cmd.Parameters.AddWithValue("@offkey", keyoff);
            //cmd.Parameters.AddWithValue("@d", date);
            //cmd.Parameters.AddWithValue("@t", time);
            //cmd.Parameters.AddWithValue("@a", ampm);
            
            if (ToolId != "")
            {
                if (new ADO.DataAdapter().RunNonQuery(cmd))
                {
                    if (keyon != "")
                    {
                        sms = "Transaction to on a tool is OK.";
                        // test
                        //CoreLibrary.Messaging mail = new CoreLibrary.Messaging();
                        //mail.SendMail("hengvongkol@gmail.com", "Hello Vongkol!");
                       
                    }
                       
                    if (keyoff != "")
                        sms = "Transaction to off a tool is OK.";
                }
                else
                    sms = "Fail to begin transaction for your tool!";
            }
            else
                sms = "The key is not correct! Make sure you type correctly.";
           
            return sms;
        }
       
    }
}